home *** CD-ROM | disk | FTP | other *** search
Text File | 1995-12-11 | 3.8 KB | 161 lines | [TEXT/MPS ] |
- /*
- File: RefCounted.cpp
-
- Contains: C++ mix-in class for doing reference counting.
-
- Written by: Troy Gaul
-
- Copyright: © 1995 by Apple Computer, Inc., all rights reserved.
- */
-
- // -- Compiler/Preprocessor Switches --
-
- #ifndef _COMPILERDEFS_
- #include "CompDefs.h"
- #endif
-
- // -- OpenDoc Utilities --
-
- #ifndef _EXCEPT_
- // Exceptions define several important macros (eg. CHECKENV)
- // which are used in the SOM method dispatch glue. If Except.h
- // is not included early enough, exceptions may not be thrown
- // correctly when returning from a SOM method with the "ev" parameter set.
- #include <Except.h>
- #endif
-
- // -- Utilites --
-
- #ifndef _REFCOUNTED_
- #include "RefCounted.h"
- #endif
-
- // -- OpenDoc Utilites --
-
- #ifndef _ODDEBUG_
- #include "ODDebug.h"
- #endif
-
- //------------------------------------------------------------------------------
- // Debugging
- //------------------------------------------------------------------------------
- #if ODDebug
-
- ODULong sNumRefCounted = 0;
-
- void CheckRefCountedObjects()
- {
- if ( sNumRefCounted != 0)
- WARN("There are %ld remaining RefCounted objects!", sNumRefCounted);
- }
-
- #endif
-
-
- //==============================================================================
- // MRefCounted
- //==============================================================================
-
- //------------------------------------------------------------------------------
- // Constructor
- //------------------------------------------------------------------------------
-
- MRefCounted::MRefCounted()
- {
- fRefCount = 1;
-
- #if ODDebug
- sNumRefCounted++;
- #endif
- }
-
- //------------------------------------------------------------------------------
- // Destructor
- //------------------------------------------------------------------------------
-
- MRefCounted::~MRefCounted()
- {
- #if ODDebug
- if ( fRefCount != 0 )
- WARN("MRefCounted deleted with ref-count of %ld.", fRefCount);
-
- sNumRefCounted--;
- #endif
- }
-
- //------------------------------------------------------------------------------
- // Acquire
- //------------------------------------------------------------------------------
-
- void MRefCounted::Acquire()
- {
- fRefCount++;
- }
-
- //------------------------------------------------------------------------------
- // Release
- //------------------------------------------------------------------------------
-
- void MRefCounted::Release()
- {
- WASSERTM(fRefCount > 0, "MRefCounted released with refcount < 1.");
- fRefCount--;
-
- if ( fRefCount == 0 )
- delete this;
- }
-
- //------------------------------------------------------------------------------
- // GetRefCount
- //------------------------------------------------------------------------------
-
- ODSLong MRefCounted::GetRefCount()
- {
- return fRefCount;
- }
-
- //==============================================================================
- // TempRefCounted
- //==============================================================================
-
- //------------------------------------------------------------------------------
- // Constructor
- //------------------------------------------------------------------------------
-
- TempRefCounted::TempRefCounted( MRefCounted* object )
- {
- fObject = object;
- }
-
- //------------------------------------------------------------------------------
- // Destructor
- //------------------------------------------------------------------------------
-
- TempRefCounted::~TempRefCounted()
- {
- if ( fObject != kODNULL )
- {
- fObject->Release();
- fObject = kODNULL;
- }
- }
-
- //------------------------------------------------------------------------------
- // operator=
- //------------------------------------------------------------------------------
-
- MRefCounted* TempRefCounted::operator=( MRefCounted* object )
- {
- fObject = object;
- return object;
- }
-
- //------------------------------------------------------------------------------
- // DontRelease
- //------------------------------------------------------------------------------
-
- void TempRefCounted::DontRelease()
- {
- fObject = kODNULL;
- }
-